﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.User;
using B.S.BasicData.Api.Read.DTO;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.Handler.User
{
    public class GetShiHandler : IRequestHandler<GetShiCommand, ApiResult<List<ShiDTO>>>
    {
        private readonly IBaseRepository<ShiModel> shiRepo;
        private readonly IMapper mapper;

        public GetShiHandler(IBaseRepository<ShiModel> shiRepo, IMapper mapper)
        {
            this.shiRepo = shiRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<List<ShiDTO>>> Handle(GetShiCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<ShiDTO>> res = new ApiResult<List<ShiDTO>>();
            var shi = shiRepo.GetAll().Where(x => x.ShengId == request.ShengId);
            res.Data = mapper.Map<List<ShiDTO>>(shi);
            res.Code = ApiEnum.Success;
            res.Msg = "显示市级信息成功";
            return Task.FromResult(res);
        }
    }
}
